import React, { useEffect } from 'react'
import PubSub from 'pubsub-js'
import Father1 from './components/Father1'
import Father2 from './components/Father2'
export default function App() {
    useEffect(()=>{
        /**
         * msg:消息名
         * data:数据
         * 组件挂载阶段订阅消息
         */
        let fanId = PubSub.subscribe('fanfan', (msg, data)=>{
            console.log('App msg: ', msg);
            console.log('App data: ', data);
        })
        let fengId = PubSub.subscribe('fengfeng', (msg, data)=>{
            console.log('App msg: ', msg);
            console.log('App data: ', data);
        })
        return ()=>{
            PubSub.unsubscribe(fanId)
            PubSub.unsubscribe(fengId)
        }
    },[])
    return (
        <div>
            <h3>App</h3>
            <p><button onClick={()=>{
                PubSub.unsubscribe('fanfan'); // 封杀凡凡
            }}>取消凡凡的消息</button></p>
            <p><button onClick={()=>{
                PubSub.clearAllSubscriptions();
            }}>封杀所有艺人</button></p>
            <hr />
            <Father1 />
            <Father2 />
        </div>
    )
}
